<?php

namespace App\services\v1;

use App\Models\LuckDraw;
use App\services\BaseServices;

class DrawServices extends BaseServices
{

    /**
     * 增加奖品
     * @param array $params
     * @return array
     */
    public function addDraw(array $params)
    {
        $insertData = [];
        foreach ($params as $field => $param) {
            $insertData[unCamelize($field)] = $param;
        }
        $addDrawRes = LuckDraw::create($insertData);
        return $this->resData;
    }

    /**
     * 奖品列表
     * @param int $activityId
     * @return array
     */
    public function getDrawList(int $activityId = 0)
    {
        $field    = ['id','name','url','probability','activity_id'];
        $drawList = LuckDraw::query()
            ->when($activityId, function ( $q) use($activityId) {
                $q->whereActivityId($activityId);
            })->paginate($this->limit, $field, 'page', $this->page)->toArray();
        $this->resData['data'] = $drawList['data'];
        $this->resData['data']['count'] = $drawList['total'];
        return $this->resData;
    }
}